var moduleUrl = "/furerp/employee/";
var baseUrl;

function bodyOnload() {
    baseUrl = document.getElementById("baseUrl").value;
    var xhr = new XMLHttpRequest;
    xhr.open("GET", baseUrl + moduleUrl + "getEmployeeList");
    xhr.onreadystatechange = function () {
        if (XMLHttpRequest.DONE === xhr.readyState) {
            if (200 === xhr.status) {
                var employeeList = eval("(" + xhr.responseText + ")");
                showTable(employeeList);
            }
        }
    }
    xhr.send();
    // xhr.setRequestHeader('Content-Type','application/json;charset=utf-8');
}

function showTable(joRe) {
    var msg = document.getElementById("message");
    var divTable = document.getElementById("divTable");
    if (null == joRe["employeeCount"] || "0" == joRe["employeeCount"]) {
        msg.innerText = "无员工记录";
        divTable.style.display = "none";
        return;
    }
    else {
        msg.innerText = "";
        filTable(joRe["employeeList"]);
        divTable.style.display = "block";
    }
}

const tableId = "tbDetail";

/*
 * 填充表格 exportField 字段对象(保存代码,名称(数组)) exportContent 表格内容对象(数组)
 */
function filTable(exportContent) {

    var tbdetail = document.getElementById(tableId);
    tbdetail.innerHTML = "";

    var row = document.createElement("tr");
    addTh(row, "编号");
    addTh(row, "姓名");
    addTh(row, "所属部门");
    addTd(row, "管理");
    tbdetail.appendChild(row);

    for (var i = 0; i < exportContent.length; i++) {
        // 循环添加行
        row = document.createElement("tr");
        // 将其中每个对象的属性取出添加列
        addTd(row, exportContent[i]["epId"]);
        addTd(row, exportContent[i]["epName"]);
        addTd(row, exportContent[i]["workshop"]);
        var delink = document.createElement("a");
        delink.href = baseUrl + "/furerp/employee/delete?epId=" + exportContent[i]["epId"];
        delink.innerText = "删除";
        addTdLink(row, delink);
        tbdetail.appendChild(row);
    }

}

function addTh(row, str) {
    var tnode = document.createTextNode(str);
    var th = document.createElement("th");
    th.setAttribute("class", "tableTitle");
    th.appendChild(tnode);
    row.appendChild(th);
}

function addTd(row, str) {
    var tnode = document.createTextNode(str);
    var td = document.createElement("td");
    td.appendChild(tnode);
    row.appendChild(td);
}

function addTdLink(row, link) {
    var td = document.createElement("td");
    td.appendChild(link);
    row.appendChild(td);
}